package com.block.account.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.block.account.model.dto.EntryAccountDTO;
import com.block.account.service.IAccountActionHandleService;
import com.block.account.service.IAccountItemChannleHisService;
import com.block.common.service.LogFactory;
import com.block.common.util.BigDecimalUtil;
import com.block.db.entity.AccountItemChannle;
import com.block.db.mapper.AccountItemChannleMapper;
import com.block.account.service.IAccountItemChannleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;

/**
 * <p>
 * 渠道账户信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-02-02
 */
@Service
public class AccountItemChannleServiceImpl extends ServiceImpl<AccountItemChannleMapper, AccountItemChannle> implements IAccountItemChannleService, IAccountActionHandleService {

    private static Logger log = LogFactory.getCommLog();


    @Autowired
    IAccountItemChannleHisService accountItemChannleHisService;

    /**
     * 获取渠道账户信息
     * @param channelCode
     * @param channelName
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public AccountItemChannle getAccountInfoByChannelCode(String channelCode, String channelName) {

        AccountItemChannle accountItemChannle = this.baseMapper.getEntityBychannelCodeLock(channelCode);
        if (ObjectUtil.isEmpty(accountItemChannle)){
            accountItemChannle = this.saveAccountInfo(channelCode, channelName);
        }
        return accountItemChannle;
    }

    public AccountItemChannle saveAccountInfo(String channelCode, String channelName){

        AccountItemChannle newEntity = new AccountItemChannle();
        newEntity.setChannelCode(channelCode);
        newEntity.setChannelName(channelName);

        boolean save = this.save(newEntity);
        return this.getById(newEntity.getId());

    }

    /**
     * 账户信息入账
     * @param entryAccountDTO
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean entryAccountInfo(EntryAccountDTO entryAccountDTO) {

        AccountItemChannle accountInfo =
                this.getById(entryAccountDTO.getItemAccountId());

        BigDecimal avaliableAmount = BigDecimalUtil.add(entryAccountDTO.getChangeAmount(), accountInfo.getAvaliableAmount());
        BigDecimal totalAmount = BigDecimalUtil.add(entryAccountDTO.getChangeAmount(), accountInfo.getTotalAmount());

        AccountItemChannle updateEntity = new AccountItemChannle();
        updateEntity.setId(accountInfo.getId());
        updateEntity.setAvaliableAmount(avaliableAmount);
        updateEntity.setTotalAmount(totalAmount);
        boolean updateRes = this.updateById(updateEntity);
        log.info("【渠道账户入账】更新渠道账户完成【updateRes】{}【channelCode】{}【channelName】{}【actionType】{}【changeAmount】{}【recordId】{}",
                updateRes, entryAccountDTO.getItemAccountKey(), entryAccountDTO.getItemAccountName(),
                entryAccountDTO.getActionType(), entryAccountDTO.getChangeAmount(), entryAccountDTO.getRecordId());
        if (!updateRes){
            return false;
        }

        entryAccountDTO.setOriginAmount(accountInfo.getAvaliableAmount());
        entryAccountDTO.setCurrentAmount(avaliableAmount);
        boolean hisRes = accountItemChannleHisService.saveChannelHisInfo( entryAccountDTO);
        log.info("【渠道账户入账】新增渠道账户历史【hisRes】{}【channelCode】{}【channelName】{}【actionType】{}【changeAmount】{}【recordId】{}",
                hisRes, entryAccountDTO.getItemAccountKey(), entryAccountDTO.getItemAccountName(),
                entryAccountDTO.getActionType(), entryAccountDTO.getChangeAmount(), entryAccountDTO.getRecordId());
        if (!hisRes){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return hisRes;
    }
}
